package com.yaorange.jk.service.impl;

import com.yaorange.jk.dao.BaseDao;
import com.yaorange.jk.entity.Factory;
import com.yaorange.jk.service.FactoryService;
import com.yaorange.jk.utils.Pagination;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author coach tam
 * @date 2017/11/7
 */
@Service("factoryService")
@Transactional
public class FactoryServiceImpl implements FactoryService {
    @Autowired
    private BaseDao<Factory,String> factoryDao;
    @Override
    @Transactional(readOnly = true)
    public List<Factory> findListByType(String type) {
        return factoryDao.getListByHQL("from Factory f where f.ctype=?",type);
    }
    @Override
    public Pagination findPage(Pagination page) {
        return factoryDao.pageByHql("from Factory",page.getPageNo(),page.getPageSize(),null);
    }

    @Override
    public List<Factory> findAll() {
        return factoryDao.getListByHQL("from Factory");
    }

    @Override
    public void save(Factory model) {
        factoryDao.save(model);
    }

    @Override
    public void deleteById(String id) {
        //factoryDao.deleteById(Factory.class,id);
        //厂家删除时，只要修改状态为2(代表这个厂家与我们公司没有合作了)
        Factory factory = factoryDao.get(Factory.class, id);
        factory.setState("2");
        factoryDao.saveOrUpdate(factory);

    }

    @Override
    public void update(Factory model) {
        factoryDao.update(model);
    }

    @Override
    public Factory findById(String id) {
        return factoryDao.get(Factory.class,id);
    }
}
